package com.laikan.reader.bookshelf.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.components.BasicApplication;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.reader.R;
import com.laikan.reader.bookreader.activity.BookDesActivity;
import com.laikan.reader.core.events.LaiKanEvent;
import com.laikan.reader.core.utils.PublicUtil;
import com.laikan.reader.core.utils.ShareSdkReadBookShare;
import com.laikan.reader.core.view.SimpleTagImageView;
import com.laikan.reader.database.Book;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Administrator on 2016/8/31.
 */
public class BookManagerActivity extends BasicActivity {

    private Book mBook;
    private String mBook_Id;

    private TextView mTVBookName, mAuthorName, mLastUpdateTime, mShow_Detail, mTV_BookDelete;
    private RelativeLayout mRL_Share;
    private SimpleTagImageView mBook_Img;
    private BitmapDrawable mShareDrawable;
    private ImageView mIVShare;
    private View mBG_View;

    @Override
    protected void assignViews() {
        setContentView(R.layout.activity_bookmanager);
        mBG_View = findViewById(R.id.bg_view);
        mTVBookName = (TextView)findViewById(R.id.tv_book_introduce_name);
        mAuthorName = (TextView)findViewById(R.id.tv_book_introduce_authorname);
        mLastUpdateTime = (TextView)findViewById(R.id.tv_book_introduce_share);
        mRL_Share = (RelativeLayout)findViewById(R.id.rl_share);
        mShow_Detail = (TextView)findViewById(R.id.show_detail);
        mTV_BookDelete = (TextView)findViewById(R.id.tv_bookdelete);
        mBook_Img = (SimpleTagImageView)findViewById(R.id.book_img);
        mIVShare = (ImageView)findViewById(R.id.iv_share);
        mShareDrawable = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.icon_share_aa), Color.parseColor("#66ccdd")));
        mIVShare.setImageDrawable(mShareDrawable);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void initData() {
        mBook = (Book)getIntent().getSerializableExtra("book");
        if (mBook != null) {
            mTVBookName.setText(mBook.getName());
            mAuthorName.setText(mBook.getAuthorName());
            if(!"".equals(mBook.getUpdateTime())){
                if(mBook.getUpdateTime() != null){
                    mLastUpdateTime.setText(getString(R.string.book_last_update_time)+PublicUtil.getDateToString(Long.parseLong(mBook.getUpdateTime())));
                }
            }else{
                mLastUpdateTime.setText(R.string.booksheft_error_uptime);
            }

            mBook_Img.setImageURI(Uri.parse(mBook.getIcon()));

            mBook_Id = mBook.getId();
            if ("限免".equals(mBook.getStatus())) {
                mBook_Img.setTagWidth(15);  // 限免
                mBook_Img.setTagText("限免");
            }else {
                mBook_Img.setTagWidth(0);
                mBook_Img.setTagText("");
            }
        }
    }

    @Override
    protected void setListener() {
        mTV_BookDelete.setOnClickListener(this);
        mShow_Detail.setOnClickListener(this);
        mBG_View.setOnClickListener(this);
        mRL_Share.setOnClickListener(this);
    }

    @Override
    protected void onClickEvent(View view) {
        super.onClickEvent(view);
        switch (view.getId()) {
            case R.id.bg_view:
                finish();
                break;
            case R.id.show_detail:
                MobclickAgent.onEvent(mContext, "sj-sy-ca-xq");//书架-首页-长按-详情
                Intent intent = new Intent();
                intent.setClass(mContext, BookDesActivity.class);
                intent.putExtra("dataId", mBook.getId());
                intent.putExtra("data", mBook);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_bookdelete:
                MobclickAgent.onEvent(mContext, "sj-sy-ca-sc");//书架-首页-长按-删除
                sendDeleteBookEvent();
                finish();
                break;
            case R.id.rl_share: // 分享
                MobclickAgent.onEvent(mContext, "sj-sy-ca-xd");//书架-首页-长按-享读
                MobclickAgent.onEvent(mContext, "bookshelf_long_share");
                ShareSdkReadBookShare.readBookShare(mBook_Id, new ShareSdkCallBackListener(), BookManagerActivity.this);
                break;
            default:
                break;
        }
    }


    class ShareSdkCallBackListener implements ShareSdkReadBookShare.ShareReadListener{

        @Override
        public void onSuccess() {
            finish();
        }

        @Override
        public void onError() {

        }
    };

    private void sendDeleteBookEvent(){
        LaiKanEvent event = LaiKanEvent.getInstance();
        event.setEventID(LaiKanEvent.EVENT_ID_LONGPRESS_CONFIRM);
        event.setElves(mBook);
        BasicApplication.postEvent(event);
    }

    @Override
    public void finish() {
        super.finish();
        this.overridePendingTransition(0, R.anim.sliding_out_from_bottom_anim);
    }
}
